package me.sdevil507.systemAssist.systemLog.service.impl;

import com.google.common.collect.Lists;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.platform.po.PlatResourcePo;
import me.sdevil507.platform.po.QPlatResourcePo;
import me.sdevil507.platform.repository.PlatResourceRepository;
import me.sdevil507.platform.service.PlatResourceService;
import me.sdevil507.services.dto.ResultDTO;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import me.sdevil507.systemAssist.systemLog.dto.SystemAccessLogParamsDto;
import me.sdevil507.systemAssist.systemLog.mapper.SystemAccessLogMapper;
import me.sdevil507.systemAssist.systemLog.po.QSystemAccessLogPo;
import me.sdevil507.systemAssist.systemLog.po.SystemAccessLogPo;
import me.sdevil507.systemAssist.systemLog.repository.SystemAccessLogRepository;
import me.sdevil507.systemAssist.systemLog.service.SystemAccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SystemAccessLogServiceImpl implements SystemAccessLogService {

    @Autowired
    private SystemAccessLogRepository systemAccessLogrepository;
    @Autowired
    private PlatResourceRepository platResourceRepository;

    @Autowired
    JPAQueryFactory jpaQueryFactory;


    @Override
    public Page<SystemAccessLogPo> readAllByPage(SystemAccessLogParamsDto searchParamsDto, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {
        QSystemAccessLogPo qSystemAccessLogPo = QSystemAccessLogPo.systemAccessLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemAccessLogPo.id.isNotNull();

        // 排序
        Sort sort = sortParamsDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "createTime"));
        }

        // 执行返回
        Pageable pageable = pageParamsDto.getPageableWithSort(sort);

        return systemAccessLogrepository.findAll(predicate, pageable);
    }

    @Override
    public List<SystemAccessLogPo> readAll(SystemAccessLogParamsDto searchParamsDto, PageParamsDTO pageDto, SortParamsDTO sortDto) {
        QSystemAccessLogPo qSystemAccessLogPo = QSystemAccessLogPo.systemAccessLogPo;

        // 开启谓语查询条件
        Predicate predicate = qSystemAccessLogPo.id.isNotNull();

        // 排序
        Sort sort = sortDto.getSort();
        if (null == sort) {
            sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime"), new Sort.Order(Sort.Direction.DESC, "uploadTime"));
        }
        return Lists.newArrayList(systemAccessLogrepository.findAll(predicate, sort).iterator());
    }

    @Override
    public SystemAccessLogPo readById(Long id) {
        return systemAccessLogrepository.findById(id).orElse(null);
    }

    @Override
    public void add(SystemAccessLogPo excepLog) {
        systemAccessLogrepository.save(excepLog);
    }

    @Override
    public List<String> getRecentModule(SystemAccessLogPo systemAccessLogPo) {
        QSystemAccessLogPo qSystemAccessLogPo = QSystemAccessLogPo.systemAccessLogPo;

        //获取访问最多的模块
        return jpaQueryFactory.select(qSystemAccessLogPo.modulePath, qSystemAccessLogPo.modulePath.count())
                .from(qSystemAccessLogPo)
                .where(qSystemAccessLogPo.userId.eq(systemAccessLogPo.getUserId()))
                .groupBy(qSystemAccessLogPo.modulePath)
                .orderBy(qSystemAccessLogPo.modulePath.count().desc())
                .limit(6)
                .fetch()
                .stream().map(tuple -> {
                    return tuple.get(qSystemAccessLogPo.modulePath);
                }).collect(Collectors.toList());
    }
}
